/**
 * 
 */
package util;

import geometry.Space;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Normalize data in a space of specified dimensions.
 * 
 */
public class Normalizer implements Constants{
	public static void normalize(String srcFile, Space destSpace,
			String destFile) {
		Space space = findCurrentSpace(srcFile);
		System.err.println("Original space dim: " + space.getMinX() + ", "
				+ space.getMinY() + "; " + space.getMaxX() + ", "
				+ space.getMaxY());
		System.err.println("Dest space dim: " + destSpace.getMinX() + ","
				+ destSpace.getMinY() + "," + destSpace.getMaxX() + ", "
				+ destSpace.getMaxY());
		assert ((space.getMaxX() - space.getMinX()) > 0);
		BufferedWriter bw;
		// NbgDatasetParser parser = new NbgDatasetParser(srcFile,
		// EntityType.OBJECT);
		

		int counter = 0;
		BufferedReader in;
		try {
			in = new BufferedReader(new FileReader(srcFile));
			bw = new BufferedWriter(new FileWriter(destFile));
			String str;
			NBGStrParser parser;
			while ((str = in.readLine()) != null) {
				parser = new NBGStrParser(str);
				// process current point for both current position and
				// approaching node
				double currX = parser.getCurrPosx();
				double currY = parser.getCurrPosy();
				double x = currX - space.getMinX();
				double proportionX = (destSpace.getMaxX() - destSpace.getMinX())
						/ (space.getMaxX() - space.getMinX());
				double proportionY = (destSpace.getMaxY() - destSpace.getMinY())
						/ (space.getMaxY() - space.getMinY());
				x = x * proportionX + destSpace.getMinX();
				double y = currY - space.getMinY();
				y = y * proportionY + destSpace.getMinY();

				double nextNodeX = parser.getApproachingJunctionx();
				double nextNodeY = parser.getApproachingJunctiony();
				double x1 = nextNodeX - space.getMinX();
				x1 = x1 * proportionX + destSpace.getMinX();
				double y1 = nextNodeY - space.getMinY();
				y1 = y1 * proportionY + destSpace.getMinY();

				// String temp = formatter(x);
				bw.write(parser.getState() + "\t" + parser.getOid() + "\t"
						+ parser.getSeqno() + "\t" + parser.getClassid() + "\t"
						+ parser.getTimepoint() + "\t" + formatter.format(x)
						+ "\t" + formatter.format(y) + "\t" + (int)parser.getSpeed()
						+ "\t" + parser.getRoadid() + "\t"
						+ formatter.format(x1) + "\t" + formatter.format(y1));
				bw.newLine();
				counter++;
			}
			in.close();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static Space findCurrentSpace(String srcFile) {
		NBGStrParser parser;
		BufferedReader in;
		double xmin = Double.MAX_VALUE;
		double ymin = Double.MAX_VALUE;
		double xmax = Double.NEGATIVE_INFINITY;
		double ymax = Double.NEGATIVE_INFINITY;

		try {
			in = new BufferedReader(new FileReader(srcFile));
			String str;
			while ((str = in.readLine()) != null) {
				parser = new NBGStrParser(str);
				if (parser.getCurrPosx() < xmin) {
					xmin = parser.getCurrPosx();
				}
				if (parser.getApproachingJunctionx() < xmin) {
					xmin = parser.getApproachingJunctionx();
				}
				if (parser.getCurrPosx() > xmax) {
					xmax = parser.getCurrPosx();
				}
				if (parser.getApproachingJunctionx() > xmax) {
					xmax = parser.getApproachingJunctionx();
				}
				if (parser.getCurrPosy() < ymin) {
					ymin = parser.getCurrPosy();
				}
				if (parser.getApproachingJunctiony() < ymin) {
					ymin = parser.getApproachingJunctiony();
				}
				if (parser.getCurrPosy() > ymax) {
					ymax = parser.getCurrPosy();
				}
				if (parser.getApproachingJunctiony() > ymax) {
					ymax = parser.getApproachingJunctiony();
				}
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		double width = xmax - xmin;
		double height = ymax - ymin;
		return new Space(xmin, ymin, width, height);
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		String usage = "Normalizer srcFile destFile x y width height";
		// String srcFile = "datasets/tigerLine_la/tiger_la.nbg";
		// String destFile = "datasets/tigerLine_la/tiger_la_normalized.nbg";

		// System.out.println(s);

		String srcFile = "d:/temp/test.txt";
		Space s = new Space(0, 0, 10000, 10000);
		String destFile = "d:/temp/test_norm.txt";
		normalize(srcFile, s, destFile);
	}

}
